<template>
    <!--地图挂载dom-->
    <div id="map">
    </div>
</template>

<script>
    //引入依赖
    import {Map, View} from 'ol'
    import XYZ from 'ol/source/XYZ'
    import {Vector as VectorSource} from 'ol/source';
    import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
    import {GeoJSON} from 'ol/format';
    import {Fill, Style, Stroke} from 'ol/style';
    import axios from 'axios'

    export default {
        name: 'ProjectFrontCoverLayer',
        data() {
            return {
                map: null,
                coverLayer: null,
            }
        },
        mounted() {

            this.$nextTick(() => {
                //初始化地图
                this.TiandiMap = new TileLayer({
                    name: "天地图影像图层",
                    source: new XYZ({
                        url: "http://t0.tianditu.com/DataServer?T=img_w&x={x}&y={y}&l={z}&tk=9893433c99cfb657e6ec3a92fe4068e5",
                    }),
                });
                this.map = new Map({
                    target: 'map',//指定挂载dom，注意必须是id
                    layers: [
                        this.TiandiMap
                    ],
                    //配置视图
                    view: new View({
                        center: [113.24981689453125, 26.126468438108688],   //视图中心位置
                        projection: "EPSG:4326", //指定投影
                        zoom: 5   //缩放到的级别
                    })
                });
                this.addCoverLayer();
                this.draw();
            })
        },
        methods: {
            // 准备反转遮罩层
            addCoverLayer() {
                let source = new VectorSource();
                // 遮罩的样式
                let style = new Style({
                    fill: new Fill({
                        color: "rgba(8,31,63,0.3)",
                    }),
                    stroke: new Stroke({
                        color: "rgb(8,91,163)",
                        width: 1
                    })
                });
                this.coverLayer = new VectorLayer({source, style});
                this.map.addLayer(this.coverLayer);
            },
            /**
             * 开始操作
             * @param data 某个行政区域的轮廓数据
             */
            draw() {
                axios.get("https://geo.datav.aliyun.com/areas_v3/bound/440100_full.json")
                    .then((response) => {
                        console.log(response);
                        let features = new GeoJSON().readFeatures(response.data);
                        this.coverLayer.getSource().addFeatures(features);
                    })
            }
        }
    }
</script>

<style scoped>
    #map {
        width: 100%;
        height: 100%;
    }
</style>
